package binary_tree.Heap.leetcode;

/**
 * @Description: 比较两个版本号
 * @Author Stark
 * @Date 2022/3/6 21:38
 **/
public class Num165_CompareVersion {
    public int compareVersion(String version1, String version2) {
        //记录字符串的读取位置
        int index1 = 0;
        int index2 = 0;
        //存储字符串的长度
        int length1 = version1.length();
        int length2 = version2.length();
        //存储修订号
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        //如果两个字符串都走到末尾没有返回时就走出循环
        while(length1 > index1 || length2 > index2){
            //存储修订号
            while(length1 > index1 && version1.charAt(index1) != '.'){
                sb1.append(version1.charAt(index1++));
            }
            while(length2 > index2 && version2.charAt(index2) != '.'){
                sb2.append(version2.charAt(index2++));
            }
            //将修订号转换为int整数
            int num1 = Integer.valueOf(sb1.toString());
            int num2 = Integer.valueOf(sb2.toString());
            //判断修订号的大小
            if (num1 > num2) {
                return 1;
            }
            if (num1 < num2) {
                return -1;
            }
            if(length1 != index1) {//没有走到末尾,将修订号清0
                index1++;
                sb1.delete(0, length1 - 1);
            }else{
                //version1走到末尾,将其修订号改为0,versin2有修订号大于0就可以返回
                sb1 = new StringBuilder("0");
            }
            if(length2 != index2) {//同理
                index2++;
                sb2.delete(0, length2 - 1);
            }else{
                sb2 = new StringBuilder("0");
            }
        }
        //版本号相同
        return 0;
    }
}
